import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { useAppStore } from "@/store/modules/app";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    component: () => import("@/views/home/index.vue")
  },
  {
    path: "/chat",
    component: () => import("@/views/chat/index.vue")
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue")
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  if (to.query.token) {
    useAppStore().saveOrUpdate(to.query.token as string);
    window.location.href = window.location.href.split("?")[0];
  }
  next();
});

export default router;
